<template>
  <el-dialog width="60%" title="修改用户信息" 
  :visible.sync="dialogVisible"
  @close="dialogClose"
  >
    <template>

      <!-- Form表单 -->
      <el-form
        :model="roleEditorUserInfo"
        :rules="rules"
        ref="addUserDialogRef"
        label-width="100px"
      >
        <!-- ID -->
        <el-form-item label="ID" prop="roleId">
          <el-input v-model="roleEditorUserInfo.roleId" disabled></el-input>
        </el-form-item>
        <!-- 角色名称 -->
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="roleEditorUserInfo.roleName"></el-input>
        </el-form-item>
        <!-- 角色描述 -->
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="roleEditorUserInfo.roleDesc"></el-input>
        </el-form-item>
      </el-form>

      <!-- 按钮区域 -->
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editorUserFormCheckFunc">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
// 网络
import { getUserInfoById, putUserUpdateInfo } from '@/network/roleControlRequest'

export default {
  data() {
    return {
      dialogVisible: false,
      roleEditorUserInfo: {},
      rules: { // 表单校验规则
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    dialogClose() {
      // 关闭dialog组件后清空表单
      this.$refs['addUserDialogRef'].resetFields();
      
    },
    editorUserFormCheckFunc() {
      this.$refs['addUserDialogRef'].validate(async val => {
        if (!val) {
          return this.$message.error('请按提示填写');
        }
        // 网络请求
        const {meta} = await putUserUpdateInfo(this.roleEditorUserInfo.roleId, this.roleEditorUserInfo)
        // console.log(meta, data); 
        if (meta.status !== 200) {
          console.log(meta);
          return this.$message.error(meta.msg);
        }

        this.$bus.$emit('getRole'); // 重新获取最新列表数据
        this.$message.success(meta.msg);
        return this.dialogVisible = false;
      })
    },
    async getUserById(id) { // 根据id获取用户信息渲染到dialog组件上
      // console.log(1);
      this.dialogVisible = true;
      const { data, meta } = await getUserInfoById(id)
      // console.log(ret);
      if (meta.status !== 200) {
        console.log(meta)
        return this.$message.error(meta.msg)
      }
      this.roleEditorUserInfo = data;
      
    },
  },
  created() {
    this.$bus.$on('tableBtnId', (id) => this.getUserById(id))
  },
  destoryed() {
    this.$bus.$on('tableBtnId')
  },
}
</script>

<style lang="scss" scoped>
</style>



